﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class NhomQuanTriController : BaseController {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            ViewData["error"] = this.TempData["error"];
            List<ds_nhomquantri> danhsach = db.ds_nhomquantri.ToList();
            foreach (ds_nhomquantri nhom in danhsach) {
                
                nhom.ghichu = Johnny.Helper.StringHelper.RemoveHTMLTag(nhom.ghichu, 200);
            }

            return View(danhsach);
        }

        public ActionResult Create()
        {
            return View();
        }

        public ActionResult Edit ( int id ) {
            ds_nhomquantri ds_nhomquantri = db.ds_nhomquantri.Single(d => d.id_nhomquantri == id);
            ds_nhomquantri.ghichu = HttpUtility.HtmlDecode(ds_nhomquantri.ghichu);
            return View(ds_nhomquantri);
        }

        #endregion

        #region CRUD Section
        [HttpPost]
        public ActionResult Create(ds_nhomquantri ds_nhomquantri)
        {
            if (ModelState.IsValid)
            {
                db.ds_nhomquantri.AddObject(ds_nhomquantri);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ds_nhomquantri.ghichu = HttpUtility.HtmlDecode(ds_nhomquantri.ghichu);
            return View(ds_nhomquantri);
        }

        [HttpPost]
        public ActionResult Edit ( ds_nhomquantri ds_nhomquantri ) {
            if (ModelState.IsValid) {
                db.ds_nhomquantri.Attach(ds_nhomquantri);
                db.ObjectStateManager.ChangeObjectState(ds_nhomquantri, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ds_nhomquantri.ghichu = HttpUtility.HtmlDecode(ds_nhomquantri.ghichu);
            return View(ds_nhomquantri);
        }

        public ActionResult Delete ( int id ) {
            ds_nhomquantri ds_nhomquantri = db.ds_nhomquantri.Single(d => d.id_nhomquantri == id);
            if (ds_nhomquantri.ds_nhanvienquanly.Count > 0) {
                this.TempData["error"] = "Xóa các thành viên thuộc nhóm quản trị này trước khi xóa nhóm!";
            } else {
                db.ds_nhomquantri.DeleteObject(ds_nhomquantri);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        #endregion


        #region Helper Section
        
        #endregion

        #region Sub Section
        #endregion

        

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
    }
}